/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJobDetailParam;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobDetailParam;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QueryJobDetailParamProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger    LOG                   = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 任务数据库服务
     */
    private DataBaseHandler dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QueryJobDetailParamProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        try {
            LOG.debug("SERVER-NETTY: one request to query job detail param is received that body=[{}]", requestXml);
            TspJobDetailParam tspJobDetailParamVO = (TspJobDetailParam) XmlUtil.fromXml(requestXml, TspJobDetailParam.class);
            RetVo<TspJobDetailParam> tspJobDetailParamRetVo = new RetVo<TspJobDetailParam>();
            Integer pageNumber = tspJobDetailParamVO.getPageNumber();
            Integer pageSize = tspJobDetailParamVO.getPageSize();
            
            JobDetailParam jobDetailParam = new JobDetailParam();
            BeanUtils.applyIf(jobDetailParam, tspJobDetailParamVO);
            
            if (pageNumber != null && pageSize != null) {
                PageInfo<JobDetailParam> jobDetailParamPageInfo = dataBaseHandler.getServiceSupport().queryJobDetailParamPage(jobDetailParam);
                PageInfo<TspJobDetailParam> tspJobDetailParamPageInfo = new PageInfo<TspJobDetailParam>();
                List<TspJobDetailParam> tspJobDetailParamList = BeanUtils.copyList(jobDetailParamPageInfo.getList(), TspJobDetailParam.class);
                BeanUtils.applyIf(tspJobDetailParamPageInfo, jobDetailParamPageInfo);
                tspJobDetailParamPageInfo.setList(tspJobDetailParamList);
                tspJobDetailParamRetVo.setPageInfo(tspJobDetailParamPageInfo);
            } else if(tspJobDetailParamVO.getId() !=  null){
                TspJobDetailParam tspJobDetailParam = new TspJobDetailParam();
                JobDetailParam rjobDetailParam = dataBaseHandler.getServiceSupport().getJobDetailParam(jobDetailParam);
                BeanUtils.applyIf(tspJobDetailParam, rjobDetailParam);
                tspJobDetailParamRetVo.setObject(tspJobDetailParam);
            }else{
                List<JobDetailParam> jobDetailParamList = dataBaseHandler.getServiceSupport().queryJobDetailParam(jobDetailParam);
                List<TspJobDetailParam> tspJobDetailParamList = BeanUtils.copyList(jobDetailParamList, TspJobDetailParam.class);
                tspJobDetailParamRetVo.setDataList(tspJobDetailParamList);
            }
            response.setBody(XmlUtil.toXml(tspJobDetailParamRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query job detail param[{}] exception:", requestXml, e);
            String responseBody = String.format("query job detail param:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
}
